import request from '@/utils/request'

export function loadAllLogs (projectId, dateLength = 'Week') {
  return request({
    url: '/project/' + projectId + '/logs',
    method: 'get',
    params: {
      'dateLength': dateLength
    }
  })
}

export function uploadFile (file, projectId) {
  let formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/project/' + projectId + '/file',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: formData
  })
}

/**
 * 更新文档内容
 * @param content 新的内容
 * @param projectId 文档所属项目ID
 * @param version 文档版本
 * @param filename 文件名
 * @param docId 文档ID
 */
export function updateDoc ({content, projectId, version, filename, docId}) {
  let formData = new FormData()
  formData.append('file', createFile(filename, content))
  formData.append('version', version)
  return request({
    url: '/project/' + projectId + '/doc/' + docId + '/content',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: formData
  })
}

export function getDocById (projectId, docId) {
  return request({
    url: '/project/' + projectId + '/doc/' + docId + '/download',
    method: 'get'
  })
}

export function getDocInfoById (projectId, docId) {
  return request({
    url: '/project/' + projectId + '/doc/' + docId,
    method: 'get'
  })
}

export function getDocInfoByUrl (url) {
  return request({
    url: '/doc/' + url + '/info',
    method: 'get'
  })
}

export function createFile (filename, content) {
  if (filename && content !== undefined) {
    return new File([content], filename, {type: 'text/plain'})
  }
  return undefined
}

export function getDocs (projectId) {
  return request({
    url: '/project/' + projectId + '/docs',
    method: 'get'
  })
}

export function getDocLogs (docId) {
  return request({
    url: '/doc/' + docId + '/logs',
    method: 'get'
  })
}

export function download (projectId, docId) {
  return request({
    url: '/project/' + projectId + '/doc/' + docId + '/download',
    method: 'get',
    responseType: 'blob'
  })
}

/**
 * 根据文档发布路径下载文档
 * @param url 文档的发布路径
 */
export function getDocByUrl (url) {
  return request({
    url: '/doc/' + url,
    method: 'get'
  })
}

/**
 * 下载文件
 * @param data 文件数据
 * @param filename 文件名
 */
export function downloadFileToUser (data, filename = 'unKnowFile') {
  if (!data) {
    return
  }
  let url = window.URL.createObjectURL(new Blob([data]))
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', filename)

  document.body.appendChild(link)
  link.click()
}

export function deleteDocById (projectId, docId) {
  return request({
    url: '/project/' + projectId + '/doc/' + docId,
    method: 'delete'
  })
}

export function publishDoc ({projectId, docId, url = undefined}) {
  let param = new FormData()
  param.append('url', url)
  return request({
    url: '/project/' + projectId + '/doc/' + docId + '/publish',
    method: 'post',
    data: param
  })
}

export function updateDocInfo ({projectId, docId, formData}) {
  return request({
    url: '/project/' + projectId + '/doc/' + docId,
    method: 'post',
    data: formData
  })
}
